package com.epoint.mobileoa.bizlogic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.app.Application;
import android.os.Process;
import android.util.Log;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;
import com.epoint.mobileoa.R;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.personalized.epointcompany.ui.kaoqing.GPSHelper.GPSCallBack;
import com.epoint.mobileoa.personalized.epointcompany.ui.kaoqing.GPSInfo;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.StringHelp;

/**
 * @author liyc
 * @time 2012-1-8 上午12:30:04
 * @annotation 全局变量
 */
public class FrameParam extends Application {
	/******************* 百度地图参数 ***********************/

	public double my_x = 31.902278900146484;
	public double my_y = 120.54867553710938;

	public static FrameParam BMParam;

	public BMapManager mBMapManager = null;// 百度MapAPI的管理类

	public String mapKey = "D0D460F33EBFD242CCB9131EC7E1B382CBAC6B7F";// 授权Key
	public boolean isRightKey = true; // 授权Key正确，验证通过

	public final static String packageName = "com.epoint.mobileoastudy";

	// 存储所有的Activity
	private List<Activity> alist = new ArrayList<Activity>();

	// 背景色
	private int backgroundcolor;

	// 当前用户登录ID
	private String loginid;

	// 当前用户登录密码
	private String password;

	// 当前用户姓名
	private String displayname;

	// 当前用户guid
	private String userguid;

	// 后台URL
	private String seamurl;

	private GPSCallBack callBack;

	public List<Activity> getAlist() {
		return alist;
	}

	public void setAlist(List<Activity> alist) {
		this.alist = alist;
	}

	public int getBackgroundcolor() {
		try {
			if (backgroundcolor == 0) {
				if (!DBHelp.getConfigValue(this, ConfigKey.backgroundcolor)
						.equals("white")
						&& !DBHelp.getConfigValue(this,
								ConfigKey.backgroundcolor).equals("background")) {
					DBHelp.setConfigValue(this, ConfigKey.backgroundcolor,
							"white");
				}
				backgroundcolor = Res.getDrawableInt(DBHelp.getConfigValue(
						this, ConfigKey.backgroundcolor));
			}
		} catch (Exception e) {
			e.printStackTrace();
//			LogHelp.Log2SDErr(e);
			return Res.getDrawableInt("bg");
		}
		return backgroundcolor;
	}

	public void setBackgroundcolor(int backgroundcolor) {
		this.backgroundcolor = backgroundcolor;
	}

	public String getLoginid() {
		if (loginid == null || loginid.equals("")) {
			loginid = DBHelp.getConfigValue(this, ConfigKey.loginid);
		}
		return loginid;
	}

	public void setLoginid(String loginid) {
		this.loginid = loginid;
	}

	public String getPassword() {
		if (password == null || password.equals("")) {
			password = DBHelp.getConfigValue(this, ConfigKey.password);
		}
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDisplayname() {
		if (displayname == null || displayname.equals("")) {
			displayname = DBHelp.getConfigValue(this, ConfigKey.displayname);
		}
		return displayname;
	}

	public void setDisplayname(String displayname) {
		this.displayname = displayname;
	}

	public String getUserguid() {
		if (userguid == null || userguid.equals("")) {
			userguid = DBHelp.getConfigValue(this, ConfigKey.userguid);
		}
		return userguid;
	}

	public void setUserguid(String userguid) {
		this.userguid = userguid;
	}

	public String getSeamurl() {
		if (seamurl == null || seamurl.equals("")) {
			seamurl = DBHelp.getConfigValue(this, ConfigKey.seamurl);
			if (seamurl.equals("")) {
				return StringHelp.getResSeamUrl(this);
			}
		}
		return seamurl;
	}

	public void setSeamurl(String seamurl) {
		this.seamurl = seamurl;
	}

	public void init() {
		alist = new ArrayList<Activity>();

		backgroundcolor = 0;

		loginid = null;

		password = null;

		displayname = null;

		userguid = null;

		seamurl = null;
	}

	public String generateUrl(String... param) {
		StringBuilder sb = new StringBuilder();
		sb.append(this.getSeamurl());
		sb.append("?requestType=" + param[0]);
		for (int i = 1; i < param.length; i++) {
			sb.append("&param" + i + "=" + param[i]);
		}
		return sb.toString();
	}
	
	public String getMsiUrl(){
		return getString(R.string.http)+FrameMethod.getIp(this, this)+"/"+getString(R.string.interfacename)+"/"+getString(R.string.msipage);
	}
	
	public String generateMsiUrl(String... param) {
		StringBuilder sb = new StringBuilder();
		sb.append(getMsiUrl());
		sb.append("?requestMethod=" + param[0]);
		for (int i = 1; i < param.length; i++) {
			sb.append("&param" + i + "=" + param[i]);
		}
		return sb.toString();
	}

	public LocationClient mLocationClient = null;
	public MyLocationListenner myListener = new MyLocationListenner();
	private GPSInfo gpsInfo;
	public boolean isGet = false;

	@Override
	public void onCreate() {
		mLocationClient = new LocationClient(this);
		mLocationClient.registerLocationListener(myListener);
		super.onCreate();
		Log.d("locSDK_Demo1",
				"... Application onCreate... pid=" + Process.myPid());
		gpsInfo = new GPSInfo();

		// 百度地图全局参数初始化
		BMParam = this;
		mBMapManager = new BMapManager(this);
		boolean isSuccess = mBMapManager.init(this.mapKey,
				new MyGeneralListener());

		if (isSuccess) {
			mBMapManager.getLocationManager().setNotifyInternal(10, 5);// 初始化地图sdk成功，设置定位监听时间
		} else {
			// 地图sdk初始化失败，不能使用sdk
		}
	}

	@Override
	// 建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
	public void onTerminate() {
		// TODO Auto-generated method stub
		if (mBMapManager != null) {
			mBMapManager.destroy();
			mBMapManager = null;
		}
		super.onTerminate();
	}

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	public static class MyGeneralListener implements MKGeneralListener {
		@Override
		public void onGetNetworkState(int iError) {
			Toast.makeText(FrameParam.BMParam.getApplicationContext(),
					"您的网络出错啦！", Toast.LENGTH_LONG).show();
		}

		@Override
		public void onGetPermissionState(int iError) {
			if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
				// 授权Key错误：
				Toast.makeText(FrameParam.BMParam.getApplicationContext(),
						"请在BMapApiDemoApp.java文件输入正确的授权Key！", Toast.LENGTH_LONG)
						.show();
				FrameParam.BMParam.isRightKey = false;
			}
		}
	}

	public void setGPSHandler(GPSCallBack back) {
		callBack = back;
	}

	/**
	 * 监听函数，又新位置的时候，格式化成字符串，输出到屏幕中
	 */
	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nerror code : ");
			sb.append(location.getLocType());
			sb.append("\nlatitude : ");
			sb.append(location.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			sb.append("\nradius : ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation) {
				sb.append("\nspeed : ");
				sb.append(location.getSpeed());
				sb.append("\nsatellite : ");
				sb.append(location.getSatelliteNumber());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
				gpsInfo.setRoadName(location.getAddrStr());
			}
			sb.append("\nsdk version : ");
			sb.append(mLocationClient.getVersion());
			mLocationClient.requestPoi();
			gpsInfo.setDetaileInfo(sb.toString());
			gpsInfo.setLocType(location.getLocType());
		}

		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
			StringBuffer sb = new StringBuffer(256);
			sb.append("Poi time : ");
			sb.append(poiLocation.getTime());
			sb.append("\nerror code : ");
			sb.append(poiLocation.getLocType());
			sb.append("\nlatitude : ");
			sb.append(poiLocation.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(poiLocation.getLongitude());
			sb.append("\nradius : ");
			sb.append(poiLocation.getRadius());
			if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
				sb.append("\naddr : ");
				sb.append(poiLocation.getAddrStr());
				gpsInfo.setRoadName(poiLocation.getAddrStr());
			}
			if (poiLocation.hasPoi()) {
				sb.append("\nPoi:");
				sb.append(poiLocation.getPoi());
				try {
					gpsInfo.setPoi(poiLocation.getPoi());
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				sb.append("noPoi information");
			}
			gpsInfo.setLocType(poiLocation.getLocType());
			if (!isGet) {
				callBack.onBack(gpsInfo);
				isGet = true;
			}
		}
	}

}
